package com.wfm.leetcode.editor.cn;

import java.util.HashMap;
import java.util.Hashtable;

/**
 * [2,7,11,15] target = 9 ,resule [0,1]
 * 方法一：
 * 创建map，遍历数组，每次判断map是否包含target-num[i],有则找到了结果，没有则put
 */
public class POne两数之和{
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public int[] twoSum(int[] nums, int target) {
        HashMap<Integer,Integer> m = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if(m.containsKey(target-nums[i])){
                return new int[]{m.get(target-nums[i]),i};
            }else{
                m.put(nums[i],i);
            }
        }
        return new int[0];
    }

    public int[] twoSum1(int[] nums, int target) {
        HashMap<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if(map.containsKey(nums[i])){
                return new int[]{map.get(nums[i]),i};
            }
            map.put(target-nums[i],i);
        }
        return new int[0];
    }
}
//leetcode submit region end(Prohibit modification and deletion)

    public static void main(String[] args) { 
        Solution solution = new POne两数之和().new Solution();
    }
}